/**
 * @author lk
 * @date 2022/6/6 13:06
 */
package com.mmall.thread;

public class BankTest02 {
    public static void main(String[] args) {
        BankTest02 bankTest02 = new BankTest02();
        bankTest02.extracted();
    }

    private void extracted() {
        Account account = new Account();
        Thread thread;
        for (int i = 0; i < 300; i++) {
            if (i % 3 != 0) {
                thread = new Thread(() -> this.spend(account, 1000));
            } else {
                thread = new Thread(() -> this.save(account, 980));
            }
            thread.start();
        }
    }

    void save(Account account, int money) {
        account.money += money;
        System.out.println(account.money);
    }

    void spend(Account account, int money) {
        if (account.money >= money) {
            account.money -= money;
            System.out.println(account.money);
        }
    }
}

class Account {
    public int money = 0;
}
